package com.pe1n.yangtaopai.view.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.pe1n.yangtaopai.R;
import com.pe1n.yangtaopai.adapter.CollectAcyivityAdapter;
import com.pe1n.yangtaopai.bean.CarBean;
import com.pe1n.yangtaopai.bean.CollectBean;
import com.pe1n.yangtaopai.bean.CollectBeanHelper;
import com.pe1n.yangtaopai.helper.IupDataListenerTwo;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.a.I;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class CollectActivity extends AppCompatActivity {
    private RelativeLayout mRelativeLayout;
    private RecyclerView mRecyclerView;
    private CollectAcyivityAdapter collectAcyivityAdapter;
    private Context mContext;//上下文
    private List<CollectBean> mList;
    private TextView mTextView;
    private String name;
    private SharedPreferences mSharePreferrnces;
    private IupDataListenerTwo iupDataListenerTwo;//收藏回调的接口
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collect);
        EventBus.getDefault().register(this);
        initView();
    }

    @Override
    protected void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    private void initView() {
        mContext=this;
        iupDataListenerTwo=new IupDataListenerTwo() {
            @Override
            public void upDataTwo(double mDouble, boolean mBoolean) {

            }

            @Override
            public void upDataBmob() {

            }

            @Override
            public void upCollectData() {
                getDataBmob(name);
            }
        };
        mTextView= (TextView) findViewById(R.id.activity_collect_textViewCollectNo);
        mList=new ArrayList<>();
        mRelativeLayout= (RelativeLayout) findViewById(R.id.activity_collect_relativeLayout_back);
        mRecyclerView= (RecyclerView) findViewById(R.id.activity_collect_recyclerView);
        collectAcyivityAdapter=new CollectAcyivityAdapter(mContext, mList,iupDataListenerTwo);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mRecyclerView.setAdapter(collectAcyivityAdapter);
        mSharePreferrnces =getSharedPreferences("myshared", 0);
        name = mSharePreferrnces.getString("shareUserName", "no");
        if(!name.equals("no")){
            getDataBmob(name);
        }
        mRelativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
    @Subscribe(sticky = true)
    public void onCollectMessage(CollectBeanHelper collectBeanHelper){
        String string=collectBeanHelper.getString();
        getDataBmob(string);
    }

    private void getDataBmob(String string) {
        //通过Bmob得到我们的数据
        BmobQuery<CollectBean> query = new BmobQuery<CollectBean>();
        query.addWhereEqualTo("nameUserName", string);
        query.findObjects(new FindListener<CollectBean>() {
            @Override
            public void done(List<CollectBean> list, BmobException e) {
                if(list.size()>0){
                    mTextView.setVisibility(View.INVISIBLE);
                }
                if( e==null){
                    mList.clear();
                    mList.addAll(list);
                    collectAcyivityAdapter.notifyDataSetChanged();
                }
                if(e!=null){
                    Toast.makeText(mContext,"失败啦!",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
